package com.weakteam.orderonline.adminconsole.gwt.client.domain;

import com.google.gwt.json.client.*;

import java.util.Date;

/**
 * @author Nail
 */
public class Order {
    private Integer id;

    private String placeName;

    private String created;

    private String status;

    private Date lastAction;

    private String content;

    public Order() {}

    public Order(JSONObject obj) {
        this.set(obj);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPlaceName() {
        return placeName;
    }

    public void setPlaceName(String placeName) {
        this.placeName = placeName;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getLastAction() {
        return lastAction;
    }

    public void setLastAction(Date lastAction) {
        this.lastAction = lastAction;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    private void set(JSONObject jsonObj) {
        JSONNumber jId = jsonObj.get("id").isNumber();
        if (jId != null) this.id = (int)jId.doubleValue();

        JSONString jPlaceName = jsonObj.get("placeName").isString();
        if (jPlaceName != null) this.placeName = jPlaceName.stringValue();

        JSONString jCreated = jsonObj.get("created").isString();
        if (jCreated != null) this.created = jCreated.stringValue();

        JSONString jStatus = jsonObj.get("status").isString();
        if (jStatus != null) this.status = jStatus.stringValue();

        JSONArray arrContent = jsonObj.get("orderContent").isArray();
            int n = arrContent.size();
            if (n > 0) {
                StringBuilder contentValue = new StringBuilder();
                for(int i = 0; i < n; i++) {
                    JSONObject item = arrContent.get(i).isObject();
                    if (item != null) {
                        JSONNumber jCount = item.get("count").isNumber();
                        if (jCount != null) contentValue.append(jCount).append("x");
                        JSONString jGoodsName = item.get("goodsName").isString();
                        if (jGoodsName != null) contentValue.append(jGoodsName).append("=");
                        JSONNumber jItemCost = item.get("itemCost").isNumber();
                        if (jItemCost != null) contentValue.append(jItemCost).append("<br/>");
                    }
                }
                this.content = contentValue.toString();
            }
    }



}
